package com.ruoyi.onlinevideo.controller;/**
 * @InterfaceName: ChatController
 * @Description:
 * @Author: YourName
 * @Date: 2023-04-14
 */

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.onlinevideo.domain.Message;
import com.ruoyi.onlinevideo.service.MessageService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @InterfaceName: ChatController
 * @Description:
 * @Author: lilirui
 * @Date: 2023-04-14
 */
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private MessageService messageService;

    @GetMapping("/getUserList")
    public AjaxResult getUserList() {
        R<List<SysUser>> getuserlist = remoteUserService.getuserlist(SecurityConstants.INNER);
        List<SysUser> sysUsers = getuserlist.getData();
        HashMap<Long, SysUser> longSysUserHashMap = new HashMap<>();
        for (SysUser sysUser : sysUsers) {
            longSysUserHashMap.put(sysUser.getUserId(), sysUser);
        }
        sysUsers.clear();
        ArrayList<MessageClass> arrayList = new ArrayList<>();
        //获取用户map
        Set<Long> longs = longSysUserHashMap.keySet();
        for (Long uid : longs) {
            if (uid.equals(SecurityUtils.getUserId())) {
                continue;
            }
            MessageClass messageClass = new MessageClass();
            SysUser user = longSysUserHashMap.get(uid);
            Message message = messageService.getrecentmsg(SecurityUtils.getUserId(), uid);
            messageClass.setUser(user);
            messageClass.setMessage(message);
            arrayList.add(messageClass);
        }
        Collections.sort(arrayList);
        //返回结果
        return AjaxResult.success(arrayList);
    }

    @ApiOperation("获取历史聊天用户列表")
    @GetMapping("/getHisList")
    public AjaxResult getHisList() {
        ArrayList<Long> uids = messageService.getHisList(SecurityUtils.getUserId());
        R<List<SysUser>> listR = remoteUserService.getuserlistByIds(uids, SecurityConstants.INNER);
        //处理最近聊天的排序
        List<SysUser> sysUsers = listR.getData();
        HashMap<Long, SysUser> longSysUserHashMap = new HashMap<>();
        for (SysUser sysUser : sysUsers) {
            longSysUserHashMap.put(sysUser.getUserId(), sysUser);
        }
        sysUsers.clear();
        ArrayList<Object> arrayList = new ArrayList<>();
        for (Long uid : uids) {
            HashMap<String, Object> hashMap = new HashMap<>();
            SysUser user = longSysUserHashMap.get(uid);
            Message message = messageService.getrecentmsg(SecurityUtils.getUserId(), uid);
            hashMap.put("user", user);
            hashMap.put("message", message);
            arrayList.add(hashMap);
        }
        return AjaxResult.success(arrayList);
    }

    @ApiOperation("获取与某个用户的聊天记录")
    @GetMapping("/getMsgList")
    public AjaxResult getMsgList(Long remoteUid) {
        List<Message> msgList = messageService.getMsgList(SecurityUtils.getUserId(), remoteUid);
        return AjaxResult.success(msgList);
    }

    @Data
    class MessageClass implements Comparable {
        private SysUser user;
        private Message message;

        @Override
        public int compareTo(@NotNull Object o) {
            if (o instanceof MessageClass) {
                MessageClass messageClass = (MessageClass) o;
                Message message1 = this.getMessage();
                if (message1==null){
                    return 1;
                }
                Message message2 = messageClass.getMessage();
                if (message2==null){
                    return -1;
                }
                if (message1.getCreateTime().getTime() > message2.getCreateTime().getTime()) {
                    return -1;
                } else {
                    return 1;
                }
            }
            return 0;
        }
    }
}
